<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\HttpCurl;
use StarCloud\Models\Doumvip\AppCookie;

class UcUserBot
{
    use HttpCurl;

    /* @var $httpClient HttpClient */
    private $httpClient = null;

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function getHeader()
    {
        return [
            'Content-Type' => 'application/json',
            'Cookie' => AppCookie::getAppCookie(AppCookie::APPID_UC_WP),
        ];
    }

    public function getHeaderUcSS()
    {
        return [
            'Content-Type' => 'application/json',
            'Cookie' => AppCookie::getAppCookie(AppCookie::APPID_UC_SS),
        ];
    }

    public function getUserList($page, $size = 10) {

        $url = "https://growthcpa.uc.cn/api/memberInfo/list";

        $body = [
            [
                "pageSize" => $size,
                "current" => $page
            ]
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['res']['data']['promoters'])) {
            return $json['res']['data']['promoters'];
        }

        return [];

    }

    public function getUcSsOrderItems($page, $day)
    {
        $day1 = date('Y-m-d', strtotime($day) + 10*86400);
        $url = "https://growthcpa.uc.cn/api/settleDetail/list";
        $body = [
            [
                "pageSize" => 10,
                "current" => $page,
                'date' => [$day.'T00:00:00.212Z', $day1.'T00:00:00.212Z'],
                'pwdids' => [],
                'ucids' => []
            ]
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeaderUcSS());

        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);
        if (isset($json['res']['data']['paymentList'])) {
            return $json['res']['data']['paymentList'];
        }

        return false;
    }


    public function getOrderItems($page, $day)
    {
        $day1 = date('Y-m-d', strtotime($day) + 10*86400);
        $url = "https://growthcpa.uc.cn/api/settleDetail/list";
        $body = [
            [
                "pageSize" => 10,
                "current" => $page,
                'date' => [$day.'T00:00:00.212Z', $day1.'T00:00:00.212Z'],
                'pwdids' => [],
                'ucids' => []
            ]
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        var_dump($json);
        if (isset($json['res']['data']['paymentList'])) {
            return $json['res']['data']['paymentList'];
        }

        return false;
    }
    public function checkUserExist($ucId) {

        $url = "https://growthcpa.uc.cn/api/memberInfo/list";

        $body = [
            [
                "pageSize" => 10,
                "current" => 1,
                'channelIds' => [],
                'ucids' => [$ucId]
            ]
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['res']['data']['promoters'][0])) {
            return $json['res']['data']['promoters'][0];
        }

        return false;
    }
}